<?php



namespace App\Http\Controllers\Api\User;


use App\Http\Controllers\Api\BasicController;
use App\Models\SiteCode;
use App\Models\User;
use App\Models\UserInfo;
use App\Repositories\ResponseCode;
use App\Repositories\ResponseMsg;
use App\Services\Sms;
use Illuminate\Support\Facades\Hash;
use Jiannei\Response\Laravel\Support\Facades\Response;
use Shopwwi\WebmanSocialite\Facade\Socialite;

class LoginController extends BasicController
{


    /**
     * @api {post} /user/login/password  手机密码登录
     * @apiName UserLoginPassword
     * @apiGroup UserLogin
     *
     * @apiParam {Number} mobile    手机号
     * @apiParam {String} password     密码
     *
     * @apiSuccess {String} uid UID
     * @apiSuccess {String} access_token Token
     * @apiSuccess {String} token_type 类型
     * @apiSuccess {String} expires_in 有效期
     */

    public function password()
    {
        $identity = $this->request->input('identity', 1);
        $data = $this->data(['mobile', 'password']);
        $where['mobile'] = $data['mobile'];
        $where['status'] = 1;
        $user = (new User())->getUser($where);
        $user = (new User())->getUser($where);
        if (empty($user)) Response::fail(ResponseMsg::LOGIN_FAIL, ResponseCode::LOGIN_FAIL);
        if (!Hash::check($data['password'], $user->password))
            Response::fail(ResponseMsg::PASSWORD_ERROR, ResponseCode::PASSWORD_ERROR);
        $result = (new User())->token($user);
        return Response::success($result);
    }


    /**
     * @api {post} /user/login/mobile 验证码登录
     * @apiName UserLoginMobile
     * @apiGroup UserLogin
     * @apiDescription 手机验证码登录
     *
     * @apiParam {Number} mobile  用户手机
     * @apiParam {Number} code  登录验证码
     *
     * @apiSuccess {String} uid UID
     * @apiSuccess {String} access_token Token
     * @apiSuccess {String} token_type 类型
     * @apiSuccess {String} expires_in 有效期
     *
     */
    public function mobile()
    {
        $data = $this->data(['mobile', 'code']);
        $where['mobile'] = $data['mobile'];
        //校验验证码是否正确
        $code = (new Sms())->check($data['mobile'], $data['code'], SiteCode::CODE_LOGIN);
        if ($code) {
            //验证码正确
            //判断用户是否存在
            $user = (new User())->getUser($where);
            if (!empty($user)) {
                //判断用户状态
                if ($user->status != 1) {
                    Response::fail(__('api.USER_FORBID'), ResponseCode::USER_FORBID);
                }
                //去登陆
                $token = (new User())->token($user);
                return Response::success($token);
            }
            $insert['mobile'] = $data['mobile'];
            $user = (new User())->createUser($insert);
            //去走注册登录
            $token = (new User())->token($user);
            return Response::success($token);
        } else {
            Response::fail(ResponseMsg::SMS_CODE_INVALID, ResponseCode::SMS_CODE_INVALID);
        }
//        $user = (new User())->getUser($where);
////        if((new Sms())->check($data['mobile'], $data['code'], SiteCode::CODE_LOGIN)){
////
////        }
////        dd(123);
//        if (empty($user))
//            Response::fail(__('api.USER_NOT_EXIST'), ResponseCode::USER_NOT_EXIST);
//        (new Sms())->check($data['mobile'], $data['code'], SiteCode::CODE_LOGIN);
//        if ($user->status != 1)
//            Response::fail(__('api.USER_FORBID'), ResponseCode::USER_FORBID);
//        $token = (new User())->token($user);
//        return Response::success($token);
    }

    /**
     * @api {post} /user/login/logout 退出登录
     * @apiName UserLoginLogout
     * @apiGroup UserLogin
     *
     * @apiHeader {String} Authorization='Bearer '
     */
    public function logout()
    {
        auth('sanctum')->user()->currentAccessToken()->delete();
        return Response::ok();
    }


    public function socialite(){

        $code = $this->request->input('code');
//        $user->getId();
//        $user->getNickname();
//        $user->getUsername();
//        $user->getName();
        return Response::success();

    }

}
